/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package amd.core.data;

import java.util.ArrayList;

/**
 *
 * @author Latifah Dwi Nurachma
 */

public class ListTermFrequency extends ArrayList<TermFrequency>{

    private int lastIndexPos = 0;

    public void add(String content){
        this.add(new TermFrequency(content, 1));
    }
    
    public void add(ListTermFrequency terms){
        int i = 0;
        while (i<terms.size()){
            if(terms.get(i).getContent().contains("http") || terms.get(i).getContent().contains("www"))
                System.out.println("term "+terms.get(i).getContent()+"ignored");
            add(terms.get(i));
            i++;
        }
    }


    @Override
    public boolean add(TermFrequency tf){
        boolean b;
        int i = 0;
        int tokenSize = 0;
        boolean isFound = false;

        int freq = tf.getFrequency();
        String content = tf.getContent();

        try{
            if(this.isEmpty()){
                super.add(tf);
            }
            else{
                tokenSize = this.size();
                while(i<tokenSize){
                    String stringCompared = get(i).getContent();
                    int freqCompared = get(i).getFrequency();

                    if(content.equalsIgnoreCase(stringCompared)){
                        tf.setFrequency(freqCompared + freq);
                        this.set(i, tf);
                        i = size();
                        isFound = true;
                    }
                    else{
                        i++;
                    }
                }
                if(!isFound){
                    super.add(tf);
                }
            }
        }
        catch(Exception e){
            b=false;
        }
        finally{
            b=true;
        }
        return b;
    }

}
